<template>
  <div class="app-container">
    <el-form :model="detailForm" ref="detailForm" size="small" :rules="rules" :inline="true" label-width="auto">
      <el-form-item label="榜单名称" prop="rankingName">
        <el-input v-model="detailForm.rankingName" placeholder="请输入修改后的榜单名称" :readonly="pageType == 'detail'" />
      </el-form-item>
      <el-form-item v-if="pageType == 'edit'">
        <el-button type="primary" size="mini" @click="handleUpdateName">保存</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="pageType == 'edit'">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:article:add']"
        >增加一行</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:article:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:article:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table v-loading="loading" :data="toplistList">
      <el-table-column label="排名" align="center" prop="sort" width="50" />
      <el-table-column label="UUID" align="center" prop="lawyerId" width="90" />
      <el-table-column label="律师" align="center" prop="lawyerFullname" width="90" />
      <el-table-column label="律所" align="center" prop="lawfirmName" width="180" />
      <el-table-column label="照片" align="center" width="60">
        <template slot-scope="scope">
          <el-avatar shape="square" :src="scope.row.lawyerCover | fullImgUrl() || 'https://static.minglvtang.com/images/placeholder/man.png'"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="职务" align="center" prop="lawyerJoblevel" width="120" />
      <el-table-column label="分组" align="center" prop="groupType" width="100">
        <template slot-scope="scope">
          <el-select v-model="scope.row.groupType" placeholder="请选择分组" @change="(e)=>handleUpdateGroupType(scope.row, e)">
            <el-option label="无" :value="0"></el-option>
            <el-option label="顶尖" :value="1"></el-option>
            <el-option label="领先" :value="2"></el-option>
            <el-option label="实力" :value="3"></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="评分" align="center" prop="rating" width="50" />
      <el-table-column label="评价" align="center" prop="evaluate" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120" v-if="pageType == 'edit'">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:article:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            class="text-red"
            icon="el-icon-remove-outline"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:article:remove']"
          >移除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="律师" prop="lawyerFullname">
          <div class="lawyer-item" v-if="form.lawyerId">
            <el-image :src="form.lawyerCover | fullImgUrl() || 'https://static.minglvtang.com/images/placeholder/man.png'" style="width: 100px; height: 126px" />
            <div class="info">
              <div>
                <div class="name">{{ form.lawyerFullname }}</div>
                <div class="desc">{{ form.lawfirmName }}</div>
                <div class="uid">{{ form.lawyerId }}</div>
              </div>
              <el-button type="primary" plain size="mini" @click="handleSelected()">选择律师</el-button>
            </div>
          </div>
          <el-button type="primary" plain size="mini" @click="handleSelected()" v-else>选择律师</el-button>
        </el-form-item>
        <el-form-item label="分组" prop="groupType">
          <el-select v-model="form.groupType" placeholder="请选择分组">
            <el-option label="无" :value="0"></el-option>
            <el-option label="顶尖" :value="1"></el-option>
            <el-option label="领先" :value="2"></el-option>
            <el-option label="实力" :value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="评分" prop="rating">
          <el-input v-model="form.rating" placeholder="请输入评分" />
        </el-form-item>
        <el-form-item label="评价" prop="evaluate">
          <el-input v-model="form.evaluate" type="textarea" :rows="3" placeholder="请输入评价" />
        </el-form-item>
        <el-form-item label="排名" prop="sort">
          <el-input-number v-model="form.sort" :min="1"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <SelectLawyer ref="selectLawyerRef" @selected="handleChanged"></SelectLawyer>
  </div>
</template>

<script>
import { getRankingListById as queryList, getItem, delItem, addItem, updateItem, getRelationLawyerList } from "@/api/mobile/rankingList"
import { getItem as getDetail, updateItem as updateDetailItem } from "@/api/mobile/toplist"

import SelectLawyer from "./SelectLawyer.vue";
export default {
  components: { SelectLawyer },
  name: "RankingListEdit",
  dicts: ['business_status'],
  data() {
    return {
      pageType: 'edit',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      toplistList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      lawyerList: [],
      // 表单参数
      form: {},
      yearMonth: null,
      // 表单校验
      rules: {
      },
      imgAction: `${process.env.VUE_APP_BASE_API}/common/commonUpload`,
      detailForm: {
        rankingName: ''
      }
    }
  },
  created() {
    this.getList()
    getRelationLawyerList().then(response => {
      this.lawyerList = response.data
    });
    const id = this.$route.query.id
    getDetail(id).then(res=>{
      this.detailForm = res.data;
    })
    if(this.$route.query.pageType){
      this.pageType = this.$route.query.pageType
    }
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true
      const id = this.$route.query.id
      queryList(id).then(response => {
        this.toplistList = response.data
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        rankingListReleaseId: this.$route.query.id,
        lawyerId: null,
        lawyerFullname: null,
        lawfirmName: null,
        lawyerJoblevel: null,
        lawyerSpeciality: null,
        rating: 5,
        evaluate: null,
        sort: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getItem(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改信息"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateItem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addItem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除信息编号为"' + ids + '"的数据项？').then(function() {
        return delItem(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/toplist/export', {
        ...this.queryParams
      }, `article_${new Date().getTime()}.xlsx`)
    },
    // 状态修改
    handleStatusChange(row) {
      let text = row.status == 0 ? "停用" : "启用"
      this.$modal.confirm(`确认要"${text}"${row.title}吗？`).then(function() {
        return updateItem({id :row.id, status: row.status})
      }).then(() => {
        this.$modal.msgSuccess(text + "成功")
      }).catch(function() {
        row.status = row.status == "0" ? 1 : 0
      })
    },
    beforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 5
      if (!isRightSize) {
        this.$message.error('文件大小超过 5MB')
      }
      let isAccept = new RegExp('image/*').test(file.type)
      if (!isAccept) {
        this.$message.error('应该选择image/*类型的文件')
      }
      return isRightSize && isAccept
    },
    uploadSuccess(res){
      this.form.img = res.path;
    },
    handleUpdateName(){
      this.$refs["detailForm"].validate(valid => {
        if (valid) {
          if (this.detailForm.id != null) {
            updateDetailItem(this.detailForm).then(response => {
              this.$modal.msgSuccess("修改成功")
            })
          }
        }
      })
    },
    handleUpdateGroupType(row, value){
      updateItem({id :row.id, groupType: value})
    },
    handleSelected(){
      this.$refs.selectLawyerRef.open()
    },
    handleChanged(data){
      this.form.lawyerId = data.id;
      this.form.lawyerFullname = data.lawyerFullname
      this.form.lawfirmName = data.lawfirmName
      this.form.lawyerJoblevel = data.lawyerJoblevel
      this.form.lawyerSpeciality = data.lawyerSpeciality
      this.form.lawyerCover = data.lawyerCover;
    }
  }
}
</script>

<style lang="scss" >
.lawyer-item {
  display: flex;
  gap: 12px;
  .info {
    line-height: 1.7;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
  }
  .name {
    font-weight: bold;
    font-size: 14px;
  }
  .desc,
  .uid{
    font-size: 12px;
    color:#666;
  }
}
</style>
